package net.net23.sysdent.client.ui;

import java.util.Map.Entry;
import java.util.Set;

import net.net23.sysdent.client.controller.AppController;
import net.net23.sysdent.client.ui.tree.T4LTreeItem;
import net.net23.sysdent.client.ui.widgets.links.LinksSearcher;
import net.net23.sysdent.client.ui.widgets.model.ContentWidget;
import net.net23.sysdent.client.ui.widgets.search.TagsSearcher;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.StackPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;

public class T4LTreeMenu extends Composite {

	private T4LLayout layoutManager;

	public T4LTreeMenu() {

		StackPanel menu = new StackPanel();
		menu.setWidth("150px");
		Tree userOptions = new Tree();
		userOptions.setAnimationEnabled(true);

		AppController.registerWidget(new LinksSearcher("links",
				AppController.images.link()));
		AppController.registerWidget(new TagsSearcher("tags",
				AppController.images.tag()));

		setupMainMenuOptions(userOptions);

		menu.add(userOptions,
				getHeaderString("Usuario", AppController.images.menu()), true);
		initWidget(menu);

		userOptions.addSelectionHandler(new SelectionHandler<TreeItem>() {
			public void onSelection(SelectionEvent<TreeItem> event) {
				TreeItem item = event.getSelectedItem();
				if (item instanceof T4LTreeItem) {
					T4LTreeItem t4lItem = (T4LTreeItem) item;
					ContentWidget contentWidget = AppController
							.getRegisteredWidgets().get(t4lItem.getKey());
					layoutManager.setContents(contentWidget);
					History.newItem(t4lItem.getKey());
				}
			}
		});

		final ValueChangeHandler<String> historyHandler = new ValueChangeHandler<String>() {
			public void onValueChange(ValueChangeEvent<String> event) {
				System.out.println(event.getValue());
			}
		};
		History.addValueChangeHandler(historyHandler);
	}

	private void setupMainMenuOptions(Tree parent) {
		Set<Entry<String, ContentWidget>> entrySet = AppController
				.getRegisteredWidgets().entrySet();
		for (Entry<String, ContentWidget> entry : entrySet) {
			ContentWidget contentWidget = entry.getValue();
			T4LTreeItem item = new T4LTreeItem(contentWidget.getKey(),
					AbstractImagePrototype.create(contentWidget.getIcon())
							.getHTML() + " " + contentWidget.getKey());
			parent.addItem(item);
		}
	}

	private String getHeaderString(String text, ImageResource image) {
		// Add the image and text to a horizontal panel
		HorizontalPanel hPanel = new HorizontalPanel();
		hPanel.setSpacing(0);
		hPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		hPanel.add(new Image(image));
		HTML headerText = new HTML(text);
		headerText.setStyleName("cw-StackPanelHeader");
		hPanel.add(headerText);
		// Return the HTML string for the panel
		return hPanel.getElement().getString();
	}

	public void setLayoutManager(T4LLayout t4lLayout) {
		this.layoutManager = t4lLayout;
	}
}
